"use client";
import Title from "antd/es/typography/Title";
import { message } from "antd";
import { listQuestionBankVoByPageUsingPost } from "@/api/questionBankController";
import QuestionBankList from "@/components/QuestionBankList";
import { useEffect, useState } from "react";
import { PageData } from "@/api/types";
import "./index.css";

/**
 * 题库列表页面
 * @constructor
 */
export default function BanksPage() {
  const [questionBankList, setQuestionBankList] = useState<API.QuestionBankVO[]>([]);

  useEffect(() => {
    const fetchQuestionBanks = async () => {
      try {
        const res = await listQuestionBankVoByPageUsingPost({
          pageSize: 200,
          sortField: "createTime",
          sortOrder: "descend",
        });
        if (res.data) {
          const pageData = res.data as unknown as PageData<API.QuestionBankVO>;
          setQuestionBankList(pageData.records ?? []);
        }
      } catch (e: any) {
        message.error("获取题库列表失败，" + e.message);
      }
    };
    fetchQuestionBanks();
  }, []);

  return (
    <div id="banksPage" className="max-width-content">
      <Title level={3}>题库大全</Title>
      <QuestionBankList questionBankList={questionBankList} />
    </div>
  );
}
